﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web;

namespace JKF.Substructure
{
    public class SysConfig
    {
        private static XElement _root;

        public static XElement Root
        {
            get
            {
                string path = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/SysConfig.xml");
                if (System.Web.HttpContext.Current.IsDebuggingEnabled)
                {
                    return System.Xml.Linq.XDocument.Load(path).FirstNode as XElement;
                }
                if (_root == null)
                {
                    var doc = System.Xml.Linq.XDocument.Load(path);
                    _root = doc.FirstNode as XElement;
                }
                return _root;
            }
        }

        public static string DateFormat = "yyyy-MM-dd";

        public static XElement RootByUrl(string url)
        {
            string path = System.Web.HttpContext.Current.Server.MapPath(url);
            return System.Xml.Linq.XDocument.Load(path).FirstNode as XElement;
        }

        public static string Phone
        {
            get
            {
                var root = Root;
                return root.Element("phone").Attribute("val").Value;
            }
        }

        /// <summary>
        /// 默认首页(因为设计问题,首页总在改,我又不想把原来的页面删除)
        /// </summary>
        public static string DefalultUrl
        {
            get
            {
                var root = Root;
                return root.Element("defaultPage").Attribute("url").Value;
            }
        }

        public static IEnumerable<string> MsgType()
        {
            var root = Root;
            return root.Element("msg").Elements("add").Select(a => a.Attribute("type").Value);
        }

        public static int PageSize
        {
            get
            {
                var root = Root;
                var v = root.Element("pageSize").Attribute("val").Value;
                return Convert.ToInt32(v);
            }
        }

        public static HttpServerUtility HttpServer
        {
            get
            {
                return System.Web.HttpContext.Current.Server;
            }
        }

        public static string MapPath(string path)
        {
            return HttpServer.MapPath(path);
        }

        public static string UploadDirPhysical
        {
            get
            {
                var root = Root;
                var v = root.Element("uploadDir").Attribute("val").Value;
                return MapPath(v);
            }
        }

        public static string UploadDir
        {
            get
            {
                var root = Root;
                return root.Element("uploadDir").Attribute("val").Value;
            }
        }



        public static IEnumerable<string> PlayExts
        {
            get
            {
                var root = Root;
                return root.Element("filePlayExt").Value.Trim().Split(';');
            }
        }

        /// <summary>
        /// 取得某节点的值
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns>节点名称路径</returns>
        public static string ElementValue(params string[] nodeNames)
        {    
            XElement el = Root;
            foreach (var a in nodeNames)
            {
                el = el.Element(a);
            }
            return el.Value.Trim();
        }

        /// <summary>
        /// 取得根节点下的某节点
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public static XElement Element(params string[] nodeNames)
        {
            XElement el = Root;
            foreach (var a in nodeNames)
            {
                el = el.Element(a);
            }
            return el;
        }
        #region 角色读取配置
        /// <summary>
        /// 总部
        /// </summary>
        public static string HeadShop
        {
            get
            {
                var root = Root;
                return root.Element("HeadShop").Attribute("val").Value;
            }
        }
        /// <summary>
        /// 总监
        /// </summary>
        // public static string Majordomo
        // {
        // get
        // {
        // var root = Root;
        // return root.Element("Majordomo").Attribute("val").Value;
        // }
        // }
        /// <summary>
        /// 区域，总监
        /// </summary>
        public static string AreaShop
        {
            get
            {
                var root = Root;
                return root.Element("AreaShop").Attribute("val").Value;
            }
        }
        /// <summary>
        /// 店长
        /// </summary>
        public static string LocalShop
        {
            get
            {
                var root = Root;
                return root.Element("LocalShop").Attribute("val").Value;
            }
        }
        /// <summary>
        /// 操作员
        /// </summary>
        public static string OperatorShop
        {
            get
            {
                var root = Root;
                return root.Element("OperatorShop").Attribute("val").Value;
            }
        } 
        #endregion

        #region 操作名称读取配置
        /// <summary>
        /// 预览
        /// </summary>
        public static string Preview
        {
            get
            {
                var root = Root;
                return root.Element("Preview").Attribute("val").Value;
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        public static string Add
        {
            get
            {
                var root = Root;
                return root.Element("Add").Attribute("val").Value;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public static string Edit
        {
            get
            {
                var root = Root;
                return root.Element("Edit").Attribute("val").Value;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public static string Delete
        {
            get
            {
                var root = Root;
                return root.Element("Delete").Attribute("val").Value;
            }
        } 
        #endregion

        #region 模块名称编码读取配置及表名称字段
        /// <summary>
        /// 通话记录模块名称的module_code编码
        /// </summary>
        public static string CALL_LOG_MODULE_CODE
        {
            get
            {
                var root = Root;
                return root.Element("CALL_LOG").Attribute("val").Value;
            }
        }
        /// <summary>
        /// 产品入库模块名称的module_code编码
        /// </summary>
        public static string STOCK_IN_MODULE_CODE
        {
            get
            {
                var root = Root;
                return root.Element("STOCK_IN").Attribute("val").Value;
            }
        }
        /// <summary>
        /// 产品出库模块名称的module_code编码
        /// </summary>
        public static string STOCK_OUT_MODULE_CODE
        {
            get
            {
                var root = Root;
                return root.Element("STOCK_OUT").Attribute("val").Value;
            }
        }
        /// <summary>
        /// 通话记录表名称
        /// </summary>
        public static string CALL_LOG = "CALL_LOG";
        /// <summary>
        /// 产品入库表名称
        /// </summary>
        public static string STOCK_IN = "STOCK_IN";
        /// <summary>
        /// 产品出库名称
        /// </summary>
        public static string STOCK_OUT = "STOCK_OUT";
        #endregion

        /// <summary>
        /// 折扣率
        /// </summary>
        public static IEnumerable<string> Discount
        {
            get
            {
                var root = Root;
                return root.Element("discount").Value.Trim(';',' ','\n','\t').Split(';');
            }
        }
    }
}
